# -*- coding: utf-8 -*-
import re, string
from django.utils.html import strip_tags
from unicodedata import normalize

#from encodings.utf_8 import encode

def get_keywords (text, max_keywords=10):#, exclusion_list):
    exclussion_list = ("que", "las", "muy", "con", u"más", "mas",
                  "este", "esta", "esto", "desde", "para",
                  "una", "uno", "unas" , "unos", "han", "a", "como", "todo")
    keywords = []
    
    #remove tags
    text = strip_tags(text)
    
    #remove punctuation
    text = re.sub('[%s]' % re.escape(string.punctuation), ' ', text)
    
    #lowercase and normalize
    text = normalize('NFKD', text).encode('ASCII', 'ignore').lower()
    
    #split in words
    list_of_words = re.split('\s+', text)

    words_count = []
    while list_of_words != []:
        word = list_of_words[0]
        if (not word in exclussion_list) and (len(word) > 3) :
            words_count.append((list_of_words.count(word), word))
        salir = False
        while (not salir):
            try:
                list_of_words.remove(word)
            except (ValueError, IndexError):
                salir = True
        
    words_count.sort(key=lambda word_count : word_count[0], reverse=True)
    
    for word in words_count:
        keywords.append(word[1])

    return keywords[0:max_keywords]

def get_keywords_text (text, max_keywords=10):
    return ", ".join(get_keywords (text, max_keywords))

def get_keywords_tags (text, max_keywords=10):
    return "%23"+ " %23".join(get_keywords (text, max_keywords))